﻿@model SayDemo.ViewModel.SayViewModel

@{
    ViewBag.Title = "Index";
    Layout = null;
}


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>微说说</title>
    <link rel="stylesheet" href="~/Content/bootstrap.min.css">
    <link rel="stylesheet" href="~/Content/tinytalk.css">
</head>

<body>

    <div class="container">
        <div class="row">
            <div class="col-sm">

            </div>
            <div class="col-sm">
                <div class="card" style="background-color:rgba(0,0,0,0);">

                    <div class="card-login">
                        <label for="" style="font-weight:900;font-size: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;欢迎用户：@Model.Username</label>
                        <img src="./img/timg.jpg" class="cakrd-img-top" alt="" style="width: 70px; height: 70px;">
                        <button type="button" class="btn btn-danger" onclick="zx()">注销</button>
                        <!-- <button type="button" style="background-image: url(../statics/img/3.jpg);">呵呵</button> -->
                        <!-- <button type="button" class="btn btn-warning" onclick="fb()">发布说说</button> -->
                        <!-- 发布说说弹出框 -->

                        <button type="button" class="btn btn-warning" data-toggle="modal"
                                data-target="#staticBackdrop">
                            发布说说
                        </button>

                        <!-- 发布说说 -->
                        <div class="modal fade" id="staticBackdrop" data-backdrop="static" data-keyboard="false"
                             tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="staticBackdropLabel">发布说说</h5>
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">&times;</span>
                                        </button>
                                    </div>
                                    <div class="modal-body">
                                        <form>
                                            <div class="form-group">
                                                <input type="text" class="form-control"
                                                       aria-describedby="passwordHelpInline" name="msgSay" id="msgSay">
                                                <!-- <small id="passwordHelpInline" class="text-muted">
                                                  Must be 8-20 characters long.
                                                </small> -->
                                            </div>
                                        </form>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                                        <button type="button" class="btn btn-primary" data-dismiss="modal"
                                                onclick="say()">
                                            发布
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- 评论 -->
                        <div class="modal fade" id="comment" tabindex="-1" aria-labelledby="exampleModalLabel"
                             aria-hidden="true">
                            <div class="modal-dialog">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="exampleModalLabel">评论</h5>
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">&times;</span>
                                        </button>
                                    </div>
                                    <div class="modal-body">
                                        <form>
                                            <div class="form-group">
                                                <input type="text" class="form-control"
                                                       aria-describedby="passwordHelpInline" id="commentText">
                                                <!-- <small id="passwordHelpInline" class="text-muted">
                                                  Must be 8-20 characters long.
                                                </small> -->
                                            </div>
                                        </form>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                                        <button type="button" class="btn btn-primary" data-dismiss="modal"
                                                onclick="saveComment()">
                                            评论
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>



                </div>
            </div>
            <div class="col-sm">

            </div>
        </div>


        <!-- 说说内容 -->
        <div class="row">
            <div class="col-sm">
            </div>
            <div class="col-sm" id="saysayMsg" style="font-weight:900; font-size: 18px;">
                @foreach (var msg in Model.Messages)
                {
                    <div class="card border-primary shadow" style="width: 50rem; background-color:rgba(0,0,0,0);">
                        <div class="card-header">
                            <div class="row">
                                <div class="col-sm-10">@msg.Content</div>
                                <div class="col-sm-2">
                                    <button type="button" class="btn btn-secondary btn-sm"
                                            data-toggle="modal" data-target="#comment"
                                            onclick="msgCommit('@msg.Id')">
                                        评论
                                    </button>
                                </div>
                            </div>
                        </div>

                        <!-- 评论谁对谁说 -->
                        <div class="card-body">
                            <ul class="list-group list-group-flush" id="@msg.Id">
                                @foreach (var comment in msg.Comments)
                                {
                                    <li class="list-group-item" style="background-color:rgba(0,0,0,0);">
                                        <blockquote class="blockquote mb-0">
                                            <p>@comment.Comment</p>
                                            <footer class="blockquote-footer">
                                                <cite title="Source Title" style="color: red;">@comment.FromUserName</cite>&nbsp;&nbsp;对
                                                <cite title="Source Title" style="color: red;">@comment.ToUserName</cite>&nbsp;&nbsp;说
                                            </footer>
                                        </blockquote>
                                    </li>
                                }

                            </ul>

                        </div>
                    </div>
                }



            </div>
            <div class="col-sm">
            </div>
        </div>






    </div>

    <script src="~/Scripts/jquery-3.6.0.min.js"></script>
    <script src="~/Scripts/bootstrap.min.js"></script>
    <script src="~/Scripts/request.js"></script>

    <script>
        let currentMsgId = '';
        function zx() {
                $.ajax({
                    url: "/users/logout",
                    type: "POST",
                    dataType: "json",
                    success: function (res) {
                        console.log(res);
                        if (res.code === 200) {
                            window.location.href = "/users/login";
                        }
                    },
                    error: function (msg) {
                        console.log('网络或者服务器有问题');
                    }
                })

        }

        function say() {
            let data = {
                msgSay: $('#msgSay').val()
            }
            request('/say', data, function (res) {
                console.log(res);
                if (res.code === 200) {
                    let data = JSON.parse(res.data);
                    console.log(data);
                    let html = `
                        <div class="card border-primary shadow" style="width: 50rem;background-color:rgba(0,0,0,0); font-weight:900;">
                            <div class="card-header">
                                <div class="row">
                                    <div class="col-sm-10">${data.content}</div>
                                    <div class="col-sm-2"><button type="button" class="btn btn-secondary btn-sm"
                                            data-toggle="modal" data-target="#comment"
                                            onclick="msgCommit(${data.id})">评论</button>
                                    </div>
                                </div>

                            </div>
                            <div class="card-body">
                                <ul class="list-group list-group-flush" id="${data.id}">

                                </ul>

                            </div>
                        </div>
                        `;

                    $("#saysayMsg").prepend(html);
                }
            }, function () {
                console.log('网络或者服务器有问题');
            });
        }

        function msgCommit(msgId) {
            currentMsgId = msgId;
        }


        function saveComment() {
            let data = {
                msgId: currentMsgId,
                content: $('#commentText').val()
            }
            request('/comment', data, res => {
                console.log(res);
                if (res.code === 200) {
                    let data = res.data;
                    console.log(data);
                    let html = `
                <li class="list-group-item" style="background-color:rgba(0,0,0,0);">
                    <blockquote class="blockquote mb-0">
                        <p>${data.content}</p>
                        <footer class="blockquote-footer" style="background-color:rgba(0,0,0,0);">
                            <cite title="Source Title" style="color: red;">${data.fromUserName}</cite>&nbsp;&nbsp;对
                            <cite title="Source Title" style="color: red;">${data.toUserName}</cite>&nbsp;&nbsp;说
                        </footer>
                    </blockquote>
                </li>
            `;
                    $("#" + currentMsgId).append(html);
                }
            }, err => {
                console.log('网络或者服务器有问题');
            })
        }

    </script>
</body>

</html>
